package com.jlcon.nas.biz.qbittorrent;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class QBittorrentService {

	private static final String QBITTORRENT_BASE_URL = "http://127.0.0.1:6883";
	private static final String LOGIN_URL = QBITTORRENT_BASE_URL + "/api/v2/auth/login";
	private static final String ADD_TORRENT_URL = QBITTORRENT_BASE_URL + "/api/v2/torrents/add";
	private static final String TORRENTS_INFO_URL = QBITTORRENT_BASE_URL + "/api/v2/torrents/info";

	private String sid;

	@Resource
	private RestTemplate restTemplate;

	public void login(String username, String password) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

		MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
		map.add("username", username);
		map.add("password", password);

		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

		ResponseEntity<String> response = restTemplate.postForEntity(LOGIN_URL, request, String.class);
		HttpHeaders responseHeaders = response.getHeaders();
		String cookie = responseHeaders.getFirst(HttpHeaders.SET_COOKIE);
		if (cookie != null) {
			sid = cookie.substring(0, cookie.indexOf(";"));
		}
	}

	public void addMagnetLink(String magnetLink) {
		if (sid == null) {
			log.error("未成功登录，无法添加磁力链接。");
			return;
		}

		HttpHeaders headers = new HttpHeaders();
		headers.set("Cookie", sid);
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

		MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
		map.add("urls", magnetLink);

		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

		restTemplate.postForEntity(ADD_TORRENT_URL, request, String.class);
	}

	/**
	 * 获取下载列表
	 * 
	 * @param filter 筛选条件，如 all, downloading, completed 等
	 * @return 包含下载列表信息的字符串
	 */
	public String getDownloadList(String filter) {
		if (sid == null) {
			log.error("未成功登录，无法获取下载列表。");
			return null;
		}

		HttpHeaders headers = new HttpHeaders();
		headers.set("Cookie", sid);

		// 构建带有筛选参数的 URL
		String url = TORRENTS_INFO_URL + "?filter=" + filter;
		HttpEntity<String> request = new HttpEntity<>(headers);

		ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
		if (response.getStatusCode() == HttpStatus.OK) {
			return response.getBody();
		} else {
			log.error("获取下载列表失败，状态码: " + response.getStatusCode());
			return null;
		}
	}

	// 提供一个默认获取所有下载任务的方法
	public String getDownloadList() {
		return getDownloadList("all");
	}
}
